package com.weigee.ktv.p.frag;

import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.weigee.ktv.m.WeigeeKtv;
import com.weigee.ktv.m.entity.SystemInformsEntity;
import com.weigee.ktv.m.retrofit.RetrofitUtil;
import com.weigee.ktv.p.TaskDetailContract;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by linde on 2017/5/10.
 * <p/>
 */
public class SystemInformsPresenterImpl implements TaskDetailContract.SystemInformsPresenter
{
    private final TaskDetailContract.SystemInformsView view;

    public SystemInformsPresenterImpl(TaskDetailContract.SystemInformsView view)
    {
        this.view = view;
    }

    @Override
    public void onCreated(Bundle savedInstanceState)
    {

    }

    @Override
    public void onViewCreated(Bundle savedInstanceState)
    {

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        onLoadListData(false);
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onResume()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void onLoadData(Object... objects)
    {

    }

    @Override
    public void onLoadListData(final boolean moreData)
    {
        RetrofitUtil.INSTANCE.getApiService().systemInformsList(1).enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                try
                {
                    final String respStr = response.body().string();
                    if (!TextUtils.isEmpty(respStr))
                    {
                        SystemInformsEntity entity = JSONObject.parseObject(respStr, SystemInformsEntity.class);
                        if (entity != null)
                        {
                            List<SystemInformsEntity.DataBean> been = entity.getData();
                            view.onResponseListData(been, moreData, been != null && been.size() >= WeigeeKtv.pageLength());
                        }
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {
                t.printStackTrace();
            }
        });
    }
}
